package com.example.administrator.yuejian2.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.bean.AttentionBean;
import com.example.administrator.yuejian2.bean.ClubBean;
import com.example.administrator.yuejian2.bean.IsAttentionBean;
import com.example.administrator.yuejian2.fragment.ClubMasterFragment;
import com.example.administrator.yuejian2.fragment.CourseFragment;
import com.example.administrator.yuejian2.model.AttentionClubModel;
import com.example.administrator.yuejian2.model.Club_DetailModel;
import com.squareup.picasso.Picasso;

import static com.example.administrator.yuejian2.other.Url.IMAGEURL;


public class Club_DetailActivity extends BaseActivity {

    private String i_id, u_id;
    private RadioButton course, clubvideo;
    private ClubMasterFragment clubVideoFragment;
    private CourseFragment courseFragment;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    private RetrofitListener<ClubBean> listener;
    private RetrofitListener<IsAttentionBean> isattentionBeanRetrofitListener;
    private RetrofitListener<AttentionBean> attentionBeanRetrofitListener, cancelattentionBeanRetrofitListener;
    private ImageView club_detail_pic;
    private Toolbar toolbar;
    private int flagattention;
    private ImageView isattentionclub,club_fanhui;
    private SharedPreferences sp;
    private TextView club_detail_name, club_detail_tel, club_detail_intro, club_detail_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club__detail);
        setSupportActionBar(toolbar);
        Intent intent = getIntent();
        i_id = (String) intent.getStringExtra("c_uid");
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        u_id = sp.getString("userid", null);
        initView();
        initEvents();

        listener = new RetrofitListener<ClubBean>() {
            @Override
            public void onSuccess(ClubBean clubBean, int flag) {
                club_detail_name.setText(clubBean.getClub_name());
                club_detail_tel.setText(clubBean.getClub_tel());
                club_detail_address.setText(clubBean.getClub_address());
                club_detail_intro.setText(clubBean.getClub_intro());
                Picasso.with(Club_DetailActivity.this).load(IMAGEURL+ clubBean.getClub_pic()).into(club_detail_pic);
            }

            @Override
            public void onFail() {

            }
        };
        isattentionBeanRetrofitListener = new RetrofitListener<IsAttentionBean>() {
            @Override
            public void onSuccess(IsAttentionBean isAttentionBean, int flag) {
                flagattention = Integer.parseInt(isAttentionBean.getIsattention());
                if (isAttentionBean.getIsattention().equals("1")) {
                    isattentionclub.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.aaa));

                }

            }

            @Override
            public void onFail() {

            }
        };

        attentionBeanRetrofitListener = new RetrofitListener<AttentionBean>() {
            @Override
            public void onSuccess(AttentionBean attentionBean, int flag) {
                if (attentionBean.getSuccess().equals("1")) {
                    flagattention = 1;
                    Toast.makeText(Club_DetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {

            }
        };

        cancelattentionBeanRetrofitListener = new RetrofitListener<AttentionBean>() {
            @Override
            public void onSuccess(AttentionBean attentionBean, int flag) {
                if (attentionBean.getSuccess().equals("1")) {
                    flagattention = 0;
                    Toast.makeText(Club_DetailActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {

            }
        };
        clubDetail();
        if (u_id==null)
        {
            Toast.makeText(Club_DetailActivity.this,"未登录无法收藏",Toast.LENGTH_SHORT).show();
        }
        else
        {
            isAttentionClub();
        }

    }

    private void initView() {
        course = findViewById(R.id.course);
        clubvideo = findViewById(R.id.clubvideo);
        club_detail_pic = findViewById(R.id.club_detail_pic);
        club_detail_name = findViewById(R.id.club_detail_name);
        club_detail_tel = findViewById(R.id.club_detail_tel);
        club_detail_intro = findViewById(R.id.club_detail_intro);
        club_detail_address = findViewById(R.id.club_detail_address);
        toolbar = findViewById(R.id.club_detail_toolbar);
        club_fanhui=findViewById(R.id.club_fanhui);
        isattentionclub = findViewById(R.id.is_attrntion_club);
    }

    private void initEvents() {
        course.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (courseFragment == null) {
                        courseFragment = new CourseFragment();
                    }
                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.detail_framelayout, courseFragment);
                    fragmentTransaction.commit();
                }
            }
        });

        clubvideo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (clubVideoFragment == null) {
                        clubVideoFragment = new ClubMasterFragment();
                    }
                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.detail_framelayout, clubVideoFragment);
                    fragmentTransaction.commit();
                }
            }
        });

        fragmentManager = getSupportFragmentManager();
        clubvideo.setChecked(true);


        isattentionclub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flagattention == 1) {
                    cancelAttentionClub();
                    isattentionclub.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.bbb));

                } else {
                    attentionClub();
                    isattentionclub.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.aaa));
                }
            }
        });

        club_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Club_DetailActivity.this,FragmentActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void clubDetail() {
        Club_DetailModel club_detailModel = new Club_DetailModel();
        club_detailModel.clubDetail(i_id, listener);
    }

    private void isAttentionClub() {
        AttentionClubModel isAttentionClubModel = new AttentionClubModel();
        isAttentionClubModel.isAttentionclub(u_id, i_id, isattentionBeanRetrofitListener);
    }

    private void cancelAttentionClub() {
        AttentionClubModel cancelAttentionClubModel = new AttentionClubModel();
        cancelAttentionClubModel.cancelAttentionClub(u_id, i_id, cancelattentionBeanRetrofitListener);
    }

    private void attentionClub() {
        AttentionClubModel attentionClubModel = new AttentionClubModel();
        attentionClubModel.attentionClub(u_id, i_id, attentionBeanRetrofitListener);
    }

}
